<?php

namespace App\Console\Commands;

use App\Models\AdminModel;
use App\Models\CourseArrangeModel;
use App\Models\StudentModel;
use App\Models\StudentSignModel;
use Illuminate\Console\Command;
use App\Tools\Fun;
use App\Tools\Wechat;
use App\Tools\SignTemplate;
use App\Models\StudentCourseModel;

class Zhang extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'zhang';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->setSignCourseId();
        return;
        // 统计每次排课消耗的总课时
        $arranges = CourseArrangeModel::where('status', 1)->get();
        foreach ($arranges as $arrange) {
            $courseNum = StudentSignModel::where('arrange_id', $arrange->id)->whereIn('result', [1,2])->sum('course_num');
            $arrange->total_course_num = $courseNum;
            $arrange->save();
        }
        return;
        
        // 计算排课到场人数
        $arranges = CourseArrangeModel::where('status', 1)->get();
        foreach ($arranges as $arrange) {
            $studentNum = StudentSignModel::where('arrange_id', $arrange->id)->whereIn('result', [1,2])->count();
            $arrange->attend_student_num = $studentNum;
            $arrange->save();
        }
        return;
        // 给签到记录添加排课ID
        $courses = CourseArrangeModel::where('status', 1)->get();
        foreach ($courses as $course) { 
            StudentSignModel::where([
                'course_id' => $course->course_id,
                'class_id' => $course->class_id,
                'date' => $course->date,
                'time' => $course->time,
                'arrange_id' => 0
            ])->update(['arrange_id' => $course->id]);
        }
        // self::sendTemplate(); // 发送模版消息
    }

    public static function sendTemplate(){
        $template = new SignTemplate();
        $template->toUser = "oNqg96oGnv6tj0OMJmli--J92ekY"; 
        $template->student = "张三";
        $template->course = "素描";
        $template->status = "正常";
        $template->remain = 24;
        $template->send();
    }

    private function setSignCourseId(){
        $students = StudentModel::all();
        foreach ($students as $student) {
            
            $studentCourses = StudentCourseModel::where('student_id', $student->id)
                ->where('pay_status', 1)
                ->orderBy('id', 'desc')
                ->get();
            if (!$studentCourses) {
                echo $student->id . ': ' . $student->name . '没有课程' . PHP_EOL;
                continue;
            }
            foreach($studentCourses as $course) {
                $student->course_id = $course->course_id;
                $signs = StudentSignModel::where('student_id', $student->id)
                ->whereIn('result', [1,2])
                ->where('course_id', $course->course_id)
                ->where("student_course_id", 0)
                ->orderBy('id', 'desc')
                ->limit($course->learned_num)
                ->get();
                if(!$signs) {
                    echo $student->id . ': ' . $student->name . '没有签到记录' . PHP_EOL;
                    continue;
                }
                foreach($signs as $sign) {
                    $sign->student_course_id = $course->id;
                    $sign->save();
                }
                
            }
        }
    }
    // 计算签到记录的课程金额
    private function calculateSignCourseMoney() {
        $students = StudentModel::all();
        foreach ($students as $student) {
            $signs = StudentSignModel::where('student_id', $student->id)->whereIn('result', [1,2])->orderBy('id', 'desc')->get();
            $stCourse = StudentCourseModel::where('student_id', $student->id)
                ->where('pay_status', 1)
                ->orderBy('id', 'desc')->first();
            if(!$stCourse) {
                echo $student->id . ': ' . $student->name . '没有课程' . PHP_EOL;
                continue;
            }
            $learnedNum = $stCourse->learned_num;
            $coursePrice = $stCourse->pay_price / ($stCourse->course_num + $stCourse->send_num);
            $num = 0;
            foreach ($signs as $sign) { 
                $num += $sign->course_num;
                if($num > $learnedNum) { // 如果统计的数量已经大于已学习的数量，则查找更早的课程
                     $stCourse = StudentCourseModel::where('student_id', $student->id)
                        ->where('pay_status', 1)
                        ->where('id', '<', $stCourse->id)
                        ->orderBy('id', 'desc')
                        ->first();
                        if($stCourse) {
                            $learnedNum += $stCourse->learned_num;
                            $coursePrice = $stCourse->pay_price / ($stCourse->course_num + $stCourse->send_num);
                        } else {
                            $num = 0;
                        }
                }
                $sign->course_money = $sign->course_num * $coursePrice;
                $sign->save();
            }
            
        }
    }
}
